package com.navi.loaddata.loadDao;

import com.navi.loaddata.core.errorLog.ErrorCode;
import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import com.navi.loaddata.core.oracleEntity.*;
import com.navi.loaddata.core.repository.impl.OracleRepositoryImpl;
import com.navi.loaddata.core.util.LoggerUtil;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xufan
 */
@Service
@Transactional(transactionManager = "thirdaryPlatformTransactionManager",
        rollbackFor = {RuntimeException.class, Exception.class})
public class SaveFileDataDao {
    private static final Logger logger = LoggerFactory.getLogger(SaveFileDataDao.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());

    @Autowired
    private OracleRepositoryImpl dao3;

    public void saveFileData(EtlConfDModel etlConfD, List<EtlGlassV> etlGlassVList, List<EtlChipV> etlChipVList, List<EtlDefectV> etlDefectVList, String glassId) throws Exception {
        Session session = dao3.getEntityManagerSession();
        String SQLFileProcedure = "";
        try {
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    SQLFileProcedure = "{Call ARRAY_FILE_LOADER.LOAD_DATA()}";
                    saveAll(EtlGlassVMapper.INSTANCE.etlGlassToArray(etlGlassVList));
                    if(null!=etlChipVList) saveAll(EtlChipVMapper.INSTANCE.etlChipToArray(etlChipVList));
                    if(null!=etlDefectVList) saveAll(EtlDefectVMapper.INSTANCE.etlDefectToArray(etlDefectVList));
//                    List<EtlArrayGlassV> list=dao3.list(EtlArrayGlassV.class,"from EtlArrayGlassV");
//                    list.forEach(glass->{
//                        System.out.println(glass.getOperatorId());
//                    });
                    break;
                case "CF":
                    SQLFileProcedure = "{Call CF_FILE_LOADER.LOAD_DATA()}";
                    saveAll(EtlGlassVMapper.INSTANCE.etlGlassToCf(etlGlassVList));
                    if(null!=etlChipVList) saveAll(EtlChipVMapper.INSTANCE.etlChipToCf(etlChipVList));
                    if(null!=etlDefectVList) saveAll(EtlDefectVMapper.INSTANCE.etlDefectToCf(etlDefectVList));
                    break;
                case "OC":
                    if ("CELL1".equals(etlConfD.getCellType())) {
                        SQLFileProcedure = "{Call CELL1_FILE_LOADER.LOAD_DATA()}";
                        saveAll(EtlGlassVMapper.INSTANCE.etlGlassToCell1(etlGlassVList));
                        if(null!=etlChipVList) saveAll(EtlChipVMapper.INSTANCE.etlChipToCell1(etlChipVList));
                        if(null!=etlDefectVList) saveAll(EtlDefectVMapper.INSTANCE.etlDefectToCell1(etlDefectVList));
                    } else if ("CELL2".equals(etlConfD.getCellType())) {
                        SQLFileProcedure = "{Call CELL2_FILE_LOADER.LOAD_DATA()}";
                        if(null!=etlGlassVList) saveAll(EtlGlassVMapper.INSTANCE.etlGlassToCell2(etlGlassVList));
                        if(null!=etlDefectVList) saveAll(EtlDefectVMapper.INSTANCE.etlDefectToCell2(etlDefectVList));
                    }
                    break;
            }
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_SAVE_ORA_DEFECT_FILE,e);
        }


//         call proc
        try {
            SQLQuery queryDefect = session.createSQLQuery(SQLFileProcedure);
            queryDefect.executeUpdate();
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_PROCEDURE_ORA_DEFECT_FILE,e);
        }

    }
    public void deleteFile(EtlConfDModel etlConfD) throws Exception {
        Session session = dao3.getEntityManagerSession();
        String hqlGlass = "";
        String hqlPanel = "";
        String hqlDefect = "";
        String hqlGlassI = "";
        String hqlGlassOld = "";
        String hqlPanelOld="";
        String hqlDefectOld="";
        try {
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    hqlGlass = "Delete FROM EtlArrayGlassV";
                    hqlPanel = "Delete FROM EtlArrayChipV";
                    hqlDefect = "Delete FROM EtlArrayDefectV";
                    hqlGlassI="Delete From LdrArrayGlassTI";
                    hqlGlassOld="Delete From LdrArrayGlassOldT";
                    hqlPanelOld="Delete From LdrArrayChipOldT";
                    hqlDefectOld="Delete From LdrArrayDefectOldT";
                    break;
                case "CF":
                    hqlGlass = "Delete FROM EtlCfGlassV";
                    hqlPanel = "Delete FROM EtlCfChipV";
                    hqlDefect = "Delete FROM EtlCfDefectV";
                    hqlGlassI="Delete From LdrCfGlassTI";
                    hqlGlassOld="Delete From LdrCfGlassOldT";
                    hqlPanelOld="Delete From LdrCfChipOldT";
                    hqlDefectOld="Delete From LdrCfDefectOldT";
                    break;
                case "OC":
                    if ("CELL1".equals(etlConfD.getCellType())) {
                        hqlGlass = "Delete FROM EtlCellComponentV";
                        hqlPanel = "Delete FROM EtlCellChipV";
                        hqlDefect = "Delete FROM EtlCellDefectV";
                        hqlGlassI="Delete From LdrCellComponentTI";
                        hqlGlassOld="Delete From LdrCellComponentOldT";
                        hqlPanelOld="Delete From LdrCellChipOldT";
                        hqlDefectOld="Delete From LdrCellDefectOldT";
                    } else if ("CELL2".equals(etlConfD.getCellType())) {
                        hqlGlass = "Delete FROM EtlCell2ChipV";
                        hqlPanel = "Delete FROM EtlCellChipV";
                        hqlDefect = "Delete FROM EtlCell2DefectV";
                        hqlGlassI="Delete From LdrCell2ChipTI";
                        hqlGlassOld="Delete From LdrCell2ChipOldT";
                        hqlPanelOld="Delete From LdrCellChipOldT";
                        hqlDefectOld="Delete From LdrCell2DefectOldT";
                    }
                    break;
            }
            Query qGlass = session.createQuery(hqlGlass);
            Query qPanel = session.createQuery(hqlPanel);
            Query qDefect = session.createQuery(hqlDefect);
            Query qGlassI = session.createQuery(hqlGlassI);
            Query qGlassOld = session.createQuery(hqlGlassOld);
            Query qPanelOld = session.createQuery(hqlPanelOld);
            Query qDefectOld = session.createQuery(hqlDefectOld);



            qGlass.executeUpdate();
            if (!"CELL2".equals(etlConfD.getCellType())) {
                qPanel.executeUpdate();
                qPanelOld.executeUpdate();
            }
            qDefect.executeUpdate();
            qGlassI.executeUpdate();
            qGlassOld.executeUpdate();
            qDefectOld.executeUpdate();


        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_DELETE_ORA_DEFECT_FILE,e);
        }
    }



    public <T> void saveAll(List<T> entities) throws Exception {
            dao3.saveAll(entities);
    }


}
